package com.nondo.dean.product.controller.api.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.ProductCategory;
import com.nondo.dean.product.model.result.AdminCategoryResult;
import com.nondo.dean.product.model.result.CategoryResult;
import com.nondo.dean.product.service.ProductCategoryService;


/**
 * 产品分类管理
 * @author zlj
 * @date 2018年3月28日
 */
@RestController("apiAdminProductCategory")
@RequestMapping("/{v}/admin/product/category")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 查看产品分类详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<CategoryResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<CategoryResult> view = new View<CategoryResult>(20000, "获取数据成功");
        
        CategoryResult categoryResult = new CategoryResult();
        if (id != null) {
            categoryResult = productCategoryService.getCategoryDetail(id);
        }

        view.setData(categoryResult);
        return view;
    }
    
    /**
     * 添加产品分类
     * @param request
     * @param response
     * @param productCategory 产品分类信息 {@link ProductCategory}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<ProductCategory> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody ProductCategory productCategory, BindingResult result) {
        View<ProductCategory> view = new View<ProductCategory>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductCategory productCategoryDb = this.productCategoryService.selectProductCategoryByName(productCategory.getName());
            if (productCategoryDb != null) {
                return View.of500Error("产品分类名称已存在");
            }
            this.productCategoryService.addProductCategory(productCategory);
        }
        return view;
    }

    /**
     * 更新产品分类
     * @param request
     * @param response
     * @param productCategory 产品分类信息 {@link ProductCategory}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<ProductCategory> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody ProductCategory productCategory, BindingResult result) {
        View<ProductCategory> view = new View<ProductCategory>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductCategory productCategoryDb = productCategoryService.selectProductCategory(productCategory.getId());
            if (productCategoryDb.getName().equals(productCategory.getName()) && productCategoryDb.getId() != productCategory.getId()) {
                return View.of500Error("产品分类名称已存在");
            }
            BeanUtils.copy(productCategory, productCategoryDb);
            this.productCategoryService.updateProductCategory(productCategoryDb);
        }

        return view;
    }

    /**
     * 获取产品分类列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<CategoryResult>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<ProductCategory> query) {
        View<Page<CategoryResult>> view = new View<Page<CategoryResult>>(20000, "获取列表成功");

        ProductCategory productCategory = query.getData();
        
        if (productCategory == null) {
            productCategory = new ProductCategory();
        }
        productCategory.setPid(0);// 一级分类

        Page<CategoryResult> page = new Page<CategoryResult>(query.getPage(), query.getLimit(), productCategoryService.count(productCategory));
        
        List<ProductCategory> productCategoryList = productCategoryService.getProductCategoryList(query.getOffset(), query.getLimit(), productCategory);
        
        List<CategoryResult> categroyResultList = new ArrayList<CategoryResult>();
        
        for (ProductCategory productCategoryDb : productCategoryList) {
            CategoryResult categoryResult = new CategoryResult();
            categoryResult = productCategoryService.getCategoryDetail(productCategoryDb.getId());
            categroyResultList.add(categoryResult);
        }
        
        page.setContent(categroyResultList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除产品分类
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        this.productCategoryService.deleteProductCategory(id);

        return view;
    }
    
    /**
     * 获取产品所有分类列表不带分页
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/all")
    public View<List<AdminCategoryResult>> categoryResultList(HttpServletRequest request, HttpServletResponse response) {
        View<List<AdminCategoryResult>> view = new View<List<AdminCategoryResult>>(20000, "获取列表成功");
        List<CategoryResult> categoryResultList= productCategoryService.getCategoryDetailAll();
        List<AdminCategoryResult> adminCategoryResultList = new ArrayList<AdminCategoryResult>();
        for (CategoryResult categoryResult : categoryResultList) {
            AdminCategoryResult adminCategoryResult = AdminCategoryResult.covertToBean(categoryResult);
            adminCategoryResultList.add(adminCategoryResult);
        }
        view.setData(adminCategoryResultList);
        return view;
    }
    
}
